<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@include file="ajax.jsp"%>
<!DOCTYPE html>
<html>
<head>
    <title>管理员登录</title>
    <script type="text/javascript" src="/static/js/ajax.js" charset="utf-8"></script>
    <meta charset="UTF-8">
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .login-container {
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            width: 300px;
            text-align: center;
        }

        input[type="text"],
        input[type="password"],
        input[type="button"] {
            width: 100%;
            padding: 10px;
            margin: 5px 0;
            box-sizing: border-box;
            border: 1px solid #ccc;
            border-radius: 3px;
        }

        input[type="button"] {
            background-color: #007bff;
            color: #fff;
            cursor: pointer;
        }
    </style>
</head>
<body style="display: flex; flex-direction: column;">
<div id="msg"></div>
<div class="login-container">
    <h2>管理员登录</h2>
    <form>
        <input type="text" name="username" placeholder="用户名" required="true"><br>
        <input type="password" name="password" placeholder="密码" required="true"><br>
        <input type="button" id="login" value="登录">
    </form>
</div>
<script>
    function adminLogin(loginUser){
        axiosInstance.post(
            '/admin/login',
            loginUser,
        ).then((res)=>{
            console.log(res.data.data);
            if (res.data.code == 200){
                localStorage.setItem('token', res.data.data.token);
                localStorage.setItem('userInfo', JSON.stringify(res.data.data));
                localStorage.setItem('role', 'admin');
                navigateTo('/admin/main');
            }else {
                console.log("登录失败");
                document.getElementById('msg').innerHTML = res.data.desc;
                setTimeout(()=>{
                    document.getElementById('msg').innerHTML = "";
                }, 2000);
            }
        }).catch((e)=>{
            console.log(e);
            console.log("登录出现异常");
            document.getElementById('msg').innerHTML = "登录出现异常";
            setTimeout(()=>{
                document.getElementById('msg').innerHTML = "";
            }, 2000);
        })
    }
    /**
     * 管理员登录请求
     */
    $('#login').on('click', function (){
        let input_username = document.getElementsByName('username')[0].value;
        let input_password = document.getElementsByName('password')[0].value;
        // console.log(input_username, input_password);
        let loginUser = {
            username : input_username,
            password : input_password
        }
        adminLogin(loginUser);
        // console.log(111);
        // let input_username = document.getElementsByName('username')[0].value;
        // let input_password = document.getElementsByName('password')[0].value;
        // console.log(input_username, input_password);
        // let loginUser = {
        //     username : input_username,
        //     password : input_password
        // }
        // let url = "/admin/login";
        // sendCommonPostAjaxRequest(url, loginUser, function (res){
        //     console.log(res);
        //     localStorage.setItem('token', res.data.token);
        //     localStorage.setItem('userInfo', res.data);
        // },function (xhr, status, error){
        //     console.log(xhr, status, error);
        //     console.log("登录失败");
        // })
    })

</script>
</body>
</html>
